//
//  TwitterClient.h
//  TuiTe
//
//  Created by 张 霄鹤 on 10-3-3.
//  Copyright 2010 xhzhang. All rights reserved.
//
//  Changes:
//     2010/09/04 - Add Twitter OAuth API support, because the Twitter API is updated by Twitter INC.
//                  Any thirdparty client must use OAuth to use Twitter service.
//

#import <Foundation/Foundation.h>

#import "Constants.h"

#import "TwRequest.h"
#import "TwResponse.h"
#import "OAuthToken.h"

@interface TwitterClient : NSObject {
	
	NSString *account;
	NSString *screen_name;
	NSString *password;
	
	NSMutableData *authData;
	
	BOOL changed;
	
	/**
	 * 网络状态提示的次数
	 */
	NSInteger netRefCount;
	
	NSArray *suservices;
	
	UIAlertView *alert;
	
	OAuthToken *tokenInfo;
	
@private
	NSString *consumerKey;
	NSString *consumerSecret;
}

@property(nonatomic, retain) NSString *account;
@property(nonatomic, retain) NSString *screen_name;
@property(nonatomic, retain) NSString *password;
@property(nonatomic, retain) OAuthToken *tokenInfo;

+ (TwitterClient *)getInstance;

- (id)initWithName:(NSString *)acc withPass:(NSString *)pass;

/**
 * 切换账号
 *
 */
- (void)changeAccount:(NSString *)acc withPass:(NSString *)pass;

/**
 * 设置网络状态引用
 * @param itype NSInteger 引用类型 0 代表关闭， 1代表开启
 */
- (void)setNetRef:(NSInteger)itype;

/**
 * RT Tweet
 *
 */
- (BOOL)doStatusesRetweet:(NSDictionary *)params;

/**
 * 提交更新信息
 * @param params 提交状态的参数
 * @return BOOL 是否提交成功
 */
- (BOOL)doStatusesUpdate:(NSDictionary *)params;

/**
 * 返回Twitter当中的十大
 * @param arrTarget NSMutableArray
 */
- (void)doTrends:(NSMutableArray *)arrTarget;

/**
 * 获取用户关系IDs
 * @param iType 获取类型，包括 friends/ids 和 followers/ids
 * @param cursor 游标，默认－1 其余的根据返回的数据获取
 * @return NSArray  存储返回的id字符串内容 其中，第0元素 next_cursor 第1元素 pervious_cursor 之后为 ids
 */
- (NSArray *)doFriendShipIDS:(NSInteger)iType withCursor:(NSString *)cursor;

/**
 * 检查是否from follow to了
 * @param from is user_a
 * @param to   is user_b
 * @return BOOL
 */
- (BOOL)doFriendShipExists:(NSString *)from withFollow:(NSString *)to;

/**
 * Follow 某人
 * @param uid 用户ID, 绝对了数字形式ID
 * @return User1 Follow之后的信息
 */
- (id)doFriendShipCreate:(NSString *)uid;

/**
 * 查询Tweets
 *
 */
- (NSArray *)doSearch:(NSString *)query withPage:(NSInteger)page;

/**
 * 查询用户
 */
- (NSArray *)doUserSearch:(NSString *)uname withPage:(NSInteger)page;

/**
 * 获取用户信息
 *
 */
- (id)doUserShow:(NSString *)uname;


/**
 * 返回用户的朋友的TimeLine
 * @param sinceID NSString
 * @return NSArray 经过解析之后的 Tweet数组
 */
- (NSArray *)doStatusesFriendTimeLineWithSinceID:(NSString *)sinceID;

/**
 * 返回用户的朋友的TimeLine, 包含Retweet
 * @param sinceID NSString
 * @return NSArray 经过解析之后的 Tweet数组
 */
- (NSArray *)doStatusesHomeTimeLineWithSinceID:(NSString *)sinceID;

/**
 * 装载提及
 * @param sinceID since_id
 * @return NSArray Tweet
 */
- (NSArray *)doStatusesMentions:(NSString *)sinceID;

/**
 * 获取statuses/public_timeline.xml
 * @return NSArray 解析之后的所有的status
 */
- (NSArray *)doStatusesPublicTimeLine;

#pragma mark -
#pragma mark OAuthSupport
/*
 Twitter OAuth
 
 1. Perform a request token request using HTTPS, POST, the following variables and the consumer secret "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98":
 oauth_callback
 - http://localhost:3005/the_dance/process_callback?service_provider_id=11
 oauth_consumer_key
 - GDdmIQH6jhtmLUypg82g
 oauth_nonce
 - QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk
 oauth_signature_method
 - HMAC-SHA1
 oauth_timestamp
 - 1272323042
 oauth_version
 - 1.0
 
 */

/**
 * 请求授权验证
 * @return NSString 是否请求成功
 */
- (NSString *)doOAuthRequestToken;

/*
 TuiTe
 
 Consumer key
 bOTAsQ6nHrBeyGdh3gTDA
 Consumer secret
 nx0K0AyciIMYVAoLiA04kUzoVXjXwG3KUUpUfbNYeKI
 */


#pragma mark -

/**
 * 调用请求，并返回结果
 * @param req TwResponse* 请求数据
 * @param tresp TwResponse** 请求处理后的结果
 * @return BOOL 是否处理成功
 */
- (BOOL)invokeTW:(TwRequest *)req retResponse:(TwResponse **)tresp;

- (NSString *)processResponse:(TwResponse *)resp;

- (NSData *)genAuthData;

// ShortenURL Service
+ (NSString *)shortenURL:(NSString *)url;

@end
